export * from './cache'
export * from './crypto'
export * from './db'
export * from './request'

/**
 * 将对象的key值下划线转为驼峰
 * @param obj 需要转换的对象
 * @returns key为驼峰格式的新对象
 */
export function toCamelCase<T extends Record<string, any>>(obj: T): Record<string, any> {
  if (obj === null || typeof obj !== 'object') {
    return obj
  }

  // 下划线转驼峰函数
  const _toCamelCase = (str: string): string =>
    str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase())

  // 递归处理对象
  if (Array.isArray(obj)) {
    return obj.map(item => toCamelCase(item)) as any
  }

  const result: Record<string, any> = {}
  for (const key in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, key)) {
      const camelKey = _toCamelCase(key)
      result[camelKey] = toCamelCase(obj[key])
    }
  }

  return result
}
